package com.jk.srb.sms.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.google.gson.Gson;
import com.jk.common.exception.Assert;
import com.jk.common.exception.BusinessException;
import com.jk.common.result.ResponseEnum;
import com.jk.srb.sms.service.SmsService;
import com.jk.srb.sms.utils.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Classname SmsServiceImpl
 * @Description TODO
 * @Date 2021/5/26 21:41
 * @Created by jk
 */
@Slf4j
@Service
public class SmsServiceImpl implements SmsService {

    public static final Gson gson = new Gson();

    // 生成客户端
    public static Client createClient() {
        try {
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(SmsProperties.KEY_ID)
                    // 您的AccessKey Secret
                    .setAccessKeySecret(SmsProperties.KEY_SECRET);
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";
            return new Client(config);
        } catch (Exception e) {
            log.error("阿里云短信发送Client创建失败");
            throw new BusinessException(ResponseEnum.ERROR);
        }
    }

    // 发送短信
    public static SendSmsResponseBody _send(String mobile, String templateCode, Map<String, Object> param) {
        try {
            // 生成json
            String jsonParam = gson.toJson(param);
            // 生成客户端对象
            Client client = createClient();
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(mobile)
                    .setSignName(SmsProperties.SIGN_NAME)
                    .setTemplateCode(templateCode)
                    .setTemplateParam(jsonParam);
            //使用客户端对象携带请求参数向远程阿里云服务器发起远程调用，并得到响应结果
            return client.sendSms(sendSmsRequest).getBody();
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            log.error("阿里云短信服务器连接失败");
            throw new BusinessException(ResponseEnum.ALIYUN_RESPONSE_ERROR);
        }
    }

    // 处理发送短信结果
    @Override
    public void send(String mobile, String templateCode, Map<String, Object> param) {
        try {
            SendSmsResponseBody responseBody = _send(mobile, templateCode, param);
            log.info("response.getBody()：" + responseBody);
            //获取响应结果
            String code = responseBody.getCode();
            String message = responseBody.getMessage();
            log.info("code：" + code + "，message：" + message);
            //业务失败的处理
            if (code.equals("isv.BUSINESS_LIMIT_CONTROL")) {
                log.error("短信频繁请求：" + mobile);
                throw new BusinessException(ResponseEnum.ALIYUN_SMS_LIMIT_CONTROL_ERROR);
            }
            // Assert.notEquals("isv.BUSINESS_LIMIT_CONTROL", code, ResponseEnum.ALIYUN_SMS_LIMIT_CONTROL_ERROR);
            Assert.equals("OK", code, ResponseEnum.ALIYUN_SMS_ERROR);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            log.error("阿里云短信发送sdk调用失败");
            throw new BusinessException(ResponseEnum.ALIYUN_SMS_ERROR, e);
        }
    }
}
